package com.jinyun.service;

import cn.hutool.core.util.StrUtil;
import com.jinyun.pojo.Order;
import com.jinyun.pojo.WayBill;
import com.jinyun.vo.LocationPoint;
import com.jinyun.vo.PageResult;
import com.jinyun.vo.WayBillQueryParam;
import com.jinyun.vo.WaybillTrack;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WayBillService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private BaiduService baiduService;

    /**
     * 查询订单列表
     * @param wayBillQueryParam
     * @return
     */
    public PageResult queryWayBillList(WayBillQueryParam wayBillQueryParam) {
        PageRequest pageRequest=PageRequest.of(wayBillQueryParam.getPage()-1,
                wayBillQueryParam.getPagesize(), Sort.by(Sort.Order.desc("orderTime")));
        PageResult pageResult = new PageResult();
        pageResult.setPageNum(wayBillQueryParam.getPage());
        pageResult.setPagesize(wayBillQueryParam.getPagesize());
        List<WayBill> wayBillList = this.mongoTemplate.find(new Query().with(pageRequest), WayBill.class);
        pageResult.setItems(wayBillList);
        return pageResult;
    }


    /**
     * 查询运单详情
     * @param waybillNumber 运单号
     * @return
     */
    public WayBill queryWayBillByWayNumber(String waybillNumber) {
        Query query=Query.query(Criteria.where("waybillNumber").is(waybillNumber));
        return this.mongoTemplate.findOne(query,WayBill.class);
    }


    /**
     * 查询运单轨迹
     * @param waybillNumber
     * @return
     */
    public Object queryWayBillTrack(String waybillNumber) {
        WayBill wayBill = this.queryWayBillByWayNumber(waybillNumber);
        String senderAddress= StrUtil.format("{},{},{},{}",
                wayBill.getSenderProvince().getName(),
                wayBill.getSenderCity().getName(),
                wayBill.getSenderCounty().getName(),
                wayBill.getSenderAddress()
                );
        double[] senderLocationPoint=
                this.baiduService.queryGeoByAddress(senderAddress);
        if (senderLocationPoint==null){
            return null;
        }
        String receiverAdress= StrUtil.format("{},{},{},{}",
                wayBill.getReceiverProvince().getName(),
                wayBill.getReceiverCity().getName(),
                wayBill.getReceiverCounty().getName(),
                wayBill.getReceiverAdress()
        );
        double[] receiverLocationPoint=
                this.baiduService.queryGeoByAddress(receiverAdress);
        if (receiverLocationPoint==null){
            return null;
        }

        //根据收件人地址和发件人地址规划路线
        String waypoints="32.069058,118.797248";// 途径点
        List<LocationPoint> locationPoints= baiduService
                .queryTrackRoutre(senderLocationPoint,receiverLocationPoint,waypoints);
        if (locationPoints==null){
            return null;
        }
        WaybillTrack waybillTrack = new WaybillTrack();
        waybillTrack.setLocationPointList(locationPoints);
        waybillTrack.setLicensePlate("京A99999");
        waybillTrack.setVehicleInformation(new LocationPoint("39.010042","117.221224"));
        return waybillTrack;
    }
}
